package com.coder520.commom.utils.sercutyUtils;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by PC on 2017/7/30.
 * 对密码进行MD5算法加密
 */
public class SercutyUtil {

    public static String encreptyPassword(String pwd) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String result = base64Encoder.encode(messageDigest.digest(pwd.getBytes("utf-8")));
        return result;
    }

    /**
     *
     * @param inputPwd 输入的密码
     * @param dbPwd  数据库中的密码
     * @throws UnsupportedEncodingException 字符集不支持异常
     * @throws NoSuchAlgorithmException 加密算法不存在异常
     */
    public static boolean checkPwd(String inputPwd , String dbPwd) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String result = encreptyPassword(inputPwd);
        if(result.equals(dbPwd)){
            return true;
        }else{
            return false;
        }
    }
}
